/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.camara.jersey.Ws;

import com.camara.jersey.Model.*;
import java.util.List;
import javax.ws.rs.*;
import com.camara.jersey.Dao.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/user")
public class UserResource {
    
    
    @GET
    @Path("{home}")
    @Produces(MediaType.TEXT_HTML)
    public void getHomePage (@Context HttpServletResponse servletResponse) throws IOException {
        servletResponse.sendRedirect("http://localhost:8080/com.camara.jersey.client");
    }

    @GET
    @Produces({MediaType.APPLICATION_JSON})
    public List<User> getAllUsersToJson() {
        List<User> listUsers = new ArrayList<User>();
        HashMap<Integer, User> hash = UserDao.getData();
        for (Map.Entry<Integer, User> e : hash.entrySet()) {
            listUsers.add(e.getValue());
        }

        return listUsers;

    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_JSON})
    public User getUser(@PathParam("id") Integer id) {
        User user = new User();
        HashMap<Integer, User> hash = UserDao.getData();
        user = hash.get(id);
        return user;
    }

    @POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void setUser(@FormParam("id") String id,
            @FormParam("login") String login, @FormParam("identifiant") String identifiant,
            @FormParam("password") String password,
            @Context HttpServletResponse servletResponse) throws IOException {
        //System.out.println ("Hahahahahahahhahahahahahahahahhahahahahahaha");
        HashMap<Integer, User> hash = UserDao.getData();
        if ((identifiant != null) && (login != null) && (password != null)) {
            Integer userId= Integer.parseInt(id);
            Integer mat= Integer.parseInt(identifiant);
            System.out.println ("++++++++++++++++++++++++++++++++++++++++++++++"
                    + "+++++++"+userId+" : "+mat+" : "+login+" :"+password);
            User user = new User(mat, login, password);
           UserDao.getData().put(userId, user);

        }
        servletResponse.sendRedirect("http://localhost:8080/com.camara.jersey.Json/user");

    }

}
